package com.minhua.jijiehao2.mainframe.launcher.finalforgetpasswordactivity.impl

import android.content.Context
import android.content.Intent
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import com.minhua.jijiehao2.R
import com.minhua.jijiehao2.base.BaseActivity
import com.minhua.jijiehao2.mainframe.launcher.finalforgetpasswordactivity.imp.IFinalForgetPasswordView
import com.minhua.jijiehao2.mainframe.launcher.loginactivity.impl.LoginActivity
import com.minhua.jijiehao2.utils.T
import kotlinx.android.synthetic.main.activity_final_forget_password.*

class FinalForgetPasswordActivity : BaseActivity<IFinalForgetPasswordView, FinalForgetPasswordPresenter>(), IFinalForgetPasswordView {


    companion object {
        fun intentStart(context: Context, phone: String, code: String) {
            val intent = Intent(context, FinalForgetPasswordActivity::class.java)
            intent.putExtra("phone", phone)
            intent.putExtra("code", code)
            context.startActivity(intent)
        }
    }

    private val textWatcher = object : TextWatcher {
        override fun afterTextChanged(s: Editable?) {
        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            tvSure.isEnabled = !(TextUtils.isEmpty(getPassword()) || TextUtils.isEmpty(getRePassword()))
        }

    }

    override fun getLayoutId(): Int = R.layout.activity_final_forget_password

    override fun getPresenter(): FinalForgetPasswordPresenter = FinalForgetPasswordPresenter()

    override fun initView() {
        tvSure.setOnClickListener { mPresenter.updatePassword() }
        password.editText.addTextChangedListener(textWatcher)
        rePassword.editText.addTextChangedListener(textWatcher)
    }

    override fun getUserName(): String = intent.getStringExtra("phone") ?: ""

    override fun getCode(): String = intent.getStringExtra("code") ?: ""

    override fun getPassword(): String = password.text.toString().trim()

    override fun getRePassword(): String = rePassword.text.toString().trim()

    override fun onUpdateSuccess() {
        val intent = Intent(this, LoginActivity::class.java)
        intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
        startActivity(intent)
        T.showShort("修改成功")
    }

    override fun onRePasswordError() {
        T.showShort("两次密码输入不一致")
    }
}
